home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].zip / MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].adf / Programming / Pascal_Tutes / Tute14.pas < prev    next >
Pascal/Delphi Source File  |  1995-01-27  |  8KB  |  258 lines

  1. {
  2.  
  3.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
  4.              «»                                                  «» 
  5.              «»                TUTORIAL FOURTEEN                 «» 
  6.              «»                                                  «» 
  7.              «»                        by                        «» 
  8.              «»                                                  «» 
  9.              «»                   Anthony Peck                   «» 
  10.              «»                                                  «» 
  11.              «»                                                  «» 
  12.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» 
  13.                                                                     
  14.  
  15.  
  16.          43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43
  17.  
  18.  
  19.                   I'm operating within acceptable parameters... 
  20.  
  21.    As well as accepting variables, procedures may define their own 
  22.    variables (and constants).  These cannot be accessed by any other part 
  23.    of the program.  They apply only within that procedure.
  24.  
  25.    For example, if we wanted a procedure to count numbers, we could define 
  26.    a variable within that procedure called COUNT, which would only be used 
  27.    in that procedure.
  28.  
  29.    Let's bung the rest of the theoretical ramblings and get stuck into some
  30.    juicy Pascal code... 
  31.  
  32.     Program MDTute14 (Input,Output);
  33.  
  34.     {    This program finds the factors of a user entered number
  35.  
  36.                 Author : A N Peck
  37.  
  38.                 Date : 31 October 1994
  39.                 
  40.     Procedures used:
  41.     
  42.         Title - prints up a fancy title on the screen
  43.         
  44.         Getnumber - prompts user for a number
  45.         
  46.         Convertnumber - makes sure the user has entered a
  47.  
  48.                 positive whole number
  49.     
  50.         Findfacts - finds the factors for the number }
  51.  
  52.     Var
  53.  
  54.     entry: real;
  55.  
  56.     number: longint;
  57.  
  58.     { ---------------------------------------------------------- }
  59.  
  60.     Procedure Title;
  61.  
  62.     { Prints up a nice title on the screen }
  63.  
  64.     Const
  65.  
  66.     tab = '     ';
  67.  
  68. {    ^
  69.     |
  70.     |__  Here a constant has been defined within the procedure.  This
  71.          constant cannot be accessed by the rest of the program.  For a
  72.          pictorial representation of the scope of locally defined
  73.          variables and constants, see Diagram_Six.
  74.  
  75.          I was going to make Diagram_Six a picture of an egret in
  76.          flight, but I was convinced by the bloke across the road
  77.          that it was beyond the scope of these tutorials...  }
  78.  
  79.     begin
  80.  
  81.     writeln;
  82.  
  83.     writeln(tab,tab,tab,'FACTOR FINDER!!');
  84.  
  85.     writeln(tab,tab,tab,'---------------');
  86.  
  87.     writeln;
  88.  
  89.     writeln(tab,'This program will find all the factors of a number');
  90.  
  91.     writeln;
  92.  
  93.     writeln(tab,'entered by the user.  For example, the factors');
  94.  
  95.     writeln;
  96.  
  97.     writeln(tab,'of 12 are 1,2,3,4,6 and 12');
  98.  
  99.     writeln;
  100.  
  101.     end; { Title }
  102.  
  103.     { ---------------------------------------------------------- }
  104.  
  105.     Procedure Getnumber (Var userchoice: real);
  106.  
  107.     { Prompts user for a number }
  108.  
  109.     begin
  110.  
  111.     writeln;
  112.  
  113.     write('     Please enter a whole number: ');
  114.  
  115.     readln(userchoice);
  116.  
  117.     end; { Getnumber }
  118.  
  119.     { ---------------------------------------------------------- }
  120.  
  121.     Procedure Convertnumber 
  122.  
  123.     (realnumber: real; Var integernumber: longint);
  124.  
  125.     { Converts a real number to a positive integer }
  126.  
  127.     begin
  128.  
  129.     integernumber := Abs(Round(realnumber));
  130.  
  131.     end; { Convertnumber }
  132.  
  133.     { ---------------------------------------------------------- }
  134.  
  135.     Procedure Findfacts (maximum: longint);
  136.  
  137.     { Finds all the factors for the number passed }
  138.  
  139.     Const
  140.  
  141.     tab = '     ';
  142.  
  143.     Var
  144.  
  145.     count: longint;
  146.  
  147. {    ^
  148.     |
  149.     |__  A locally defined variable is defined here.  The variable
  150.         "count" is just used within this procedure to keep track of the
  151.         factors, and as a herbicide.  }
  152.  
  153.     begin
  154.     
  155.     writeln;
  156.  
  157.     writeln(tab,'The factors of ',maximum,' are:');
  158.  
  159.     writeln;
  160.     
  161.     write(tab);
  162.  
  163.     For count := 1 to maximum do
  164.  
  165.         begin
  166.  
  167.         if (maximum mod count = 0) then 
  168.  
  169.         write(count,' ');
  170.  
  171.         end;
  172.  
  173. {        ^
  174.         |
  175.         |__  The "engine" of this program, the algorithm that
  176.              finds the factors.  If the number divided by "count"
  177.              yields no remainder, then that number must be a
  178.              factor, and is written.  If not, the "For" loop
  179.              increments one and the next "count" is checked.
  180.  
  181.              I'm just so amazed at how wonderful this algorithm is
  182.              that I think I'll set up a small shrine down in the
  183.              garden to celebrate its existence...  }
  184.  
  185.     writeln;
  186.  
  187.     writeln;
  188.  
  189.     end; { Findfacts }
  190.  
  191.     { ---------------------------------------------------------- }
  192.     { ---------------------------------------------------------- }
  193.  
  194.     begin { Main Program }
  195.  
  196.     entry := 0.00;
  197.  
  198.     number := 0;
  199.  
  200.     Title;
  201.  
  202.     Getnumber(entry);
  203.  
  204.     Convertnumber(entry,number);
  205.  
  206.     Findfacts(number);
  207.  
  208.     end. { Main Program }
  209.  
  210.     { ---------------------------------------------------------- }
  211.  
  212. {  It's fair to say that there are other (more efficient) ways of writing 
  213.    this program.  However, the point of this is a demonstration of scope.  
  214.    If you want to fiddle with the algorithm, please feel free to do so in 
  215.    the privacy of your own home.
  216.  
  217.    For small programs, such discussions of scope seem a little out of 
  218.    place, however it won't be long before you are writing programs which 
  219.    are several hundred lines in length.  It will help you debug such 
  220.    programs if by then you have a clear understanding of MODULARITY 
  221.    (keeping the program chopped into definable sections) and SCOPE.  It 
  222.    will also help if you can stick two lit incense sticks in your nostrils 
  223.    and whistle "Waltzing Matilda" at the same time!  
  224.  
  225.  
  226.                                            
  227.                                             
  228.                                              
  229.                                             
  230.                                             
  231.                                        
  232.                                       
  233.                                       
  234.                                       
  235.                                       
  236.                                        
  237.                                                      
  238.                                            
  239.                                                   
  240.                                                         
  241.                                                            
  242.                                                              
  243.                                                                
  244.            «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
  245.            «»                                                  «» 
  246.            «»           grail link elk trophus riddle          «» 
  247.            «»                                                  «» 
  248.            «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» 
  249.  
  250.  
  251.          43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43
  252.  
  253.  
  254.                                                                    }
  255.  
  256.  
  257.  
  258.